package cn.bocweb.spinning.view.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.FeedbackBean;
import cn.bocweb.spinning.model.Bean.MyFeedBackBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.FeedBackAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.ContainsEmojiEditText;
import de.greenrobot.event.EventBus;

public class FeedBackActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.feedback_list)
    ListView feedbackList;
    @Bind(R.id.info_detail_comment)
    ContainsEmojiEditText infoDetailComment;
    @Bind(R.id.feedback_send)
    Button feedbackSend;

    UserPresenter presenter;
    FeedBackAdapter adapter;
    String userid;
    List<MyFeedBackBean.DataEntity> myFeedBackBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        myFeedBackBeanList = new ArrayList<>();

        initToolbar();
        initModel();
        initClickBtn();

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initClickBtn() {
        feedbackSend.setOnClickListener(this);
    }

    private void initModel() {
        adapter = new FeedBackAdapter(this, myFeedBackBeanList);
        feedbackList.setAdapter(adapter);
        UserBean.DataEntity user = new UserBean.DataEntity();
        user.readInfoFromLocal(this);
        userid = user.getUserid();
        presenter = new UserPresenterImp();
        presenter.myFeedback(userid);

        adapter.setRefresh(new FeedBackAdapter.refresh() {
            @Override
            public void refresh() {
                feedbackList.post(new Runnable() {
                    @Override
                    public void run() {
                        feedbackList.setSelection(adapter.getCount() - 1);
                    }
                });
            }
        });

    }

    private void initToolbar() {
        toolbar.setTitle("反馈详情");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.feedback_send: {
                presenter.feedBack(userid, infoDetailComment.getText().toString());
                DeviceUtil.Log(infoDetailComment.getText().toString());
                break;
            }
        }
    }

    public void onEventMainThread(MyFeedBackBean bean) {
        if (bean.getReturn_code() == 0) {
            myFeedBackBeanList.addAll(bean.getData());
            adapter.notifyDataSetChanged();
            feedbackList.post(new Runnable() {
                @Override
                public void run() {
                    feedbackList.setSelection(adapter.getCount() - 1);
                }
            });

        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }

    public void onEventMainThread(FeedbackBean bean) {
        if (bean.getReturn_code() == 0) {
            final MyFeedBackBean.DataEntity dataEntity = new MyFeedBackBean.DataEntity();
            dataEntity.setApply_time("");
            dataEntity.setQuestion(infoDetailComment.getText().toString());
            dataEntity.setResponse_time("");
            dataEntity.setResponse("感谢您的回复,客户会稍后为您解答");
            myFeedBackBeanList.add(dataEntity);
            adapter.notifyDataSetChanged();
            infoDetailComment.setText("");

            feedbackList.post(new Runnable() {
                @Override
                public void run() {
                    feedbackList.setSelection(adapter.getCount() - 1);
                }
            });
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }
}
